package com.wu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.mapper.ArticleClassifyMapper;
import com.wu.mapper.ClassifyMapper;
import com.wu.pojo.Classify;
import com.wu.pojo.view.ClassifyView;
import com.wu.service.ArticleClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-19 20:42
 */
@Service
public class ArticleClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ArticleClassifyService {
    @Autowired
    private ArticleClassifyMapper articleClassifyMapper;

    @Override
    public List<ClassifyView> getClassifyWithArticleNumber(String classifyName, String userId) {
        if (StringUtils.isEmpty(classifyName)) {
            classifyName = null;
        }
        List<ClassifyView> classifyViewList;
        try {
            classifyViewList = baseMapper.getClassifyWithArticleNumber(classifyName, userId);
        } catch (Exception e) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "获取分类失败，请联系管理员");
        }
        return classifyViewList;
    }

    @Override
    public Classify getClassifyByArticle(String articleId) {
        if (StringUtils.isEmpty(articleId)) {
            throw new BusinessException(EnumBusinessError.PARAMETER_INVALID_ERROR);
        }
        return articleClassifyMapper.getClassifyByArticle(articleId);
    }
}
